package com.atguigu.gmall.realtime.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.RedisClient;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.async.RedisAsyncCommands;
import org.apache.commons.lang3.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

/**
 * @author Felix
 * @date 2023/12/28
 * 操作Redis的工具类
 * 在Redis配置文件中建议的配置
 *      bind
 *      protected-mode no
 *      daemonize yes
 *      日志和落盘目录建议放到有操作权限的目录下
 *      logfile /opt/module/redis-6.0.8/redis.log
 *      dir /opt/module/redis-6.0.8
 */
public class RedisUtil {
    private static JedisPool jedisPool;

    static {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMinIdle(5);
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setMaxIdle(5);
        jedisPoolConfig.setBlockWhenExhausted(true);
        jedisPoolConfig.setMaxWaitMillis(2000L);
        jedisPoolConfig.setTestOnBorrow(true);
        jedisPool = new JedisPool(jedisPoolConfig,"hadoop102",6379,10000);
    }

    //获取Jedis客户端
    public static Jedis getRedisClient(){
        System.out.println("~~~开启Jedis~~~");
        Jedis jedis = jedisPool.getResource();
        return jedis;
    }
    //关闭Jedis客户端
    public static void closeRedisClient(Jedis jedis){
        if(jedis != null){
            System.out.println("~~~关闭Jedis~~~");
            jedis.close();
        }
    }

    //获取支持异步操作的Redis连接对象
    public static StatefulRedisConnection<String,String> getAsyncRedisConnection(){
        System.out.println("~~~获取支持异步操作的Redis连接对象~~~");
        RedisClient redisClient = RedisClient.create("redis://hadoop102:6379/0");
        StatefulRedisConnection<String, String> asyncRedisConn = redisClient.connect();
        return asyncRedisConn;
    }
    //关闭支持异步操作的Redis连接对象
    public static void closeAsyncRedisConnection(StatefulRedisConnection<String, String> asyncRedisConn){
        System.out.println("~~~关闭支持异步操作的Redis连接对象~~~");
        if(asyncRedisConn != null && asyncRedisConn.isOpen()){
            asyncRedisConn.close();
        }
    }

    /**
     * 以异步的方式从Redis中获取数据
     * @param asyncRedisConn        异步连接对象
     * @param redisKey              操作Redis的key
     * @return
     */
    public static JSONObject asyncGetDimInfo(StatefulRedisConnection<String, String> asyncRedisConn,String redisKey){
        RedisAsyncCommands<String, String> asyncCommands = asyncRedisConn.async();
        try {
            String dimJsonStr = asyncCommands.get(redisKey).get();
            if(StringUtils.isNotEmpty(dimJsonStr)){
                return JSON.parseObject(dimJsonStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 以异步的方式向Redis中写入数据
     * @param asyncRedisConn        异步连接对象
     * @param redisKey              操作Redis的key
     * @param dimJsonObj            要写入到Redis中的维度对象
     */
    public static void asyncWriteDim(StatefulRedisConnection<String, String> asyncRedisConn,String redisKey,JSONObject dimJsonObj){
        RedisAsyncCommands<String, String> asyncCommands = asyncRedisConn.async();
        asyncCommands.setex(redisKey,24*3600,dimJsonObj.toJSONString());
    }


    public static void main(String[] args) {
        Jedis jedis = getRedisClient();
        String pong = jedis.ping();
        System.out.println(pong);
        closeRedisClient(jedis);
    }
}
